<?php
class OeZend_Widget_Grid_Source_ZendDbSelect extends OeZend_Widget_Grid_Source {

	private $_source;//Zend_Db_Select
	private $_sourceOrigin;//Zend_Db_Select

	private $_orderBy;
	private $_orderColumn;

	private $_page;
	private $_totalNumberPage;
	private $_startPage;
	private $_endPage;
	private $_currentPage;
	private $_linePerPage;

	public function __construct(Zend_Db_Select $select) {
		$this->setSource($select);
	}

	public function setSource($source) {
		if(!$source instanceof Zend_Db_Select) {
			throw new Zend_Exception("Source in not instanceof Zend_Db_Select");
		}
		$this->_source 		 = $source;
		$this->_sourceOrigin = clone $source;
	}

	public function getData() {
		$orderBy 	 = $this->_orderBy;
		$orderColumn = $this->_orderColumn;

		if($this->_linePerPage != 0) {
			$this->_source->limitPage($this->_page, $this->_linePerPage);
		}
		if($orderColumn && $orderBy) {
			$this->_source->reset(Zend_Db_Select::ORDER);
			$this->_source->order(" $orderColumn $orderBy ");
		}

		return $this->_source->query()->fetchAll();
	}

	public function distinctColumn($key) {
		if(!$key) {
			return $this->_sourceOrigin;
		}

		$this->_sourceOrigin->reset(Zend_Db_Select::COLUMNS)
						    ->reset(Zend_Db_Select::LIMIT_COUNT)
						    ->reset(Zend_Db_Select::LIMIT_OFFSET)
						    ->reset(Zend_Db_Select::ORDER)
						    ->reset(Zend_Db_Select::GROUP)
						    ->reset(Zend_Db_Select::HAVING)
						    ->columns(array('KEY_COLUMN_DISTINCT' => $key))
						    ->distinct(true);

		return $this->_sourceOrigin;
	}

	public function getTotalRow() {
		$rs = $this->_source->reset(Zend_Db_Select::COLUMNS)
							->reset(Zend_Db_Select::LIMIT_COUNT)
							->reset(Zend_Db_Select::LIMIT_OFFSET)
							->reset(Zend_Db_Select::ORDER)
							->reset(Zend_Db_Select::GROUP)
							->reset(Zend_Db_Select::HAVING)
							->columns("count(1) as count")
							->distinct(true)
							->query()
							->fetch();

		return isset($rs["count"]) ? $rs["count"] : 0;
	}

	public function getPage() {
		return $this->_page;
	}

	public function getTotalNumberPage() {
		return $this->_totalNumberPage;
	}

	public function getStartPage() {
		return $this->_startPage;
	}

	public function getEndPage() {
		return $this->_endPage;
	}

	public function getCurrentPage() {
		return $this->_currentPage;
	}

	public function getLinePerPage() {
		return $this->_linePerPage;
	}

	public function setPage($page) {
		$this->_page = $page;
	}

	public function setTotalNumberPage($totalNumberPage) {
		$this->_totalNumberPage = $totalNumberPage;
	}

	public function setStartPage($startPage) {
		$this->_startPage = $startPage;
	}

	public function setEndPage($endPage) {
		$this->_endPage = $endPage;
	}

	public function setCurrentPage($currentPage) {
		$this->_currentPage = $currentPage;
	}

	public function setLinePerPage($linePerPage) {
		$this->_linePerPage = $linePerPage;
	}

	function setOrderBy($orderBy) {
		$this->_orderBy = $orderBy;
	}

	function setOrderColumn($orderColumn) {
		$this->_orderColumn = $orderColumn;
	}

	public function like($key, $value) {
		if($value) {
			$key = $this->getKeyByAlias($key);
			$this->_source->where($key . '  LIKE ? ', "%". $value ."%");
		}
	}

	public function equal($key, $value) {
		if($value) {
			$key = $this->getKeyByAlias($key);
			$this->_source->where(" $key =? ", $value);
		}
	}

	public function greater($key, $value) {
		if($value) {
			$key = $this->getKeyByAlias($key);
			$this->_source->where(" $key > ? ", $value);
		}
	}

	public function lower($key, $value) {
		if($value) {
			$key = $this->getKeyByAlias($key);
			$this->_source->where(" $key < ? ", $value);
		}
	}

	public function greaterOrEqual($key, $value) {
		if($value) {
			$key = $this->getKeyByAlias($key);
			$this->_source->where(" $key >= ? ", $value);
		}
	}

	public function lowerOrEqual($key, $value) {
		if($value) {
			$key = $this->getKeyByAlias($key);
			$this->_source->where(" $key <= ? ", $value);
		}
	}

	public function getKeyByAlias($key) {
		$columns = $this->_source->getPart(Zend_Db_Select::COLUMNS);

		foreach ($columns as $value) {
			if($value[2] == $key) {
				if($value[1] instanceof Zend_Db_Expr) {
					return $value[1];
				}
				return $value[0]. '.' .$value[1];
			}
		}

		return $key;
	}
}